<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 评论编辑组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Comments_Edit extends Widget_Abstract_Comments implements Widget_Interface_Do
{
    /**
     * 插入评论
     *
     * @access public
     * @return void
     */
    public function insertComment($values = array())
    {
        /** 插件接口 */
		//HOOK $values | PLUG_Comment_Insert($values, $this);

        /** 回复评论 */
        $insertId = $this->insert($values);

        if ($insertId > 0) {
		
            /** 更新内容数 */
		    !empty($values['uid']) && $this->db->update('table.users')
		        ->where('uid = ?', $values['uid'])
	            ->expression('commentsNum', 'commentsNum + 1')
			    ->affectedRows();

            /** 更新内容数 */
		    !empty($values['cid']) && $this->db->update('table.contents')
		        ->where('cid = ?', $values['cid'])
		        ->expression('commentsNum', 'commentsNum + 1')
		        ->affectedRows();

		    /** 压入数据 */
            $this->select()
                ->where('coid = ?', $insertId)
		        ->limit(1)
			    ->fetchRow(array($this, 'push'));

            /** 插件接口 */
	        //HOOK PLUG_Comment_Insert_Success($this);
		
		}

		return $insertId;
    }
	
    /**
     * 编辑评论
     *
     * @access public
     * @return void
     */
    public function updateComment($values = array())
    {
		if (!$this->have()) {
		    return ;
		}
		
		/** 插件接口 */
		//HOOK $values | PLUG_Comment_Update($values, $this);
		
        $coid = $this->coid;

        /** 更新评论 */
        $updateRows = $this->update($coid, $values);

		/** 压入数据 */
        $this->select()
            ->where('coid = ?', $coid)
		    ->limit(1)
		    ->fetchRow(array($this, 'push'));

        /** 插件接口 */
	    //HOOK PLUG_Comment_Update_Success($this);

		return $updateRows;
    }

    /**
     * 删除评论
     *
     * @access public
     * @return void
     */
    public function deleteComment($coid)
    {
        $deleteRows = false;
        $comment = $this->select()
            ->where('coid = ?', $coid)
			->limit(1)
			->fetchRow(array($this, 'push'));

        if ($this->isWriteable()) {
			
            /** 插件接口 */			
			//HOOK PLUG_Comment_Delete($this);

            /** 更新内容数 */
		    $this->db->update('table.users')
		        ->where('uid = ?', $comment['uid'])
				->expression('commentsNum', 'commentsNum - 1')
				->affectedRows();

            /** 更新内容数 */
		    $this->db->update('table.contents')
		        ->where('cid = ?', $comment['cid'])
				->expression('commentsNum', 'commentsNum - 1')
				->affectedRows();
				
            /** 删除评论 */    
            $deleteRows = $this->delete($coid);
			
			/** 插件接口 */
			//HOOK PLUG_Comment_Delete_Success($this);

        }
        
		return $deleteRows;
    }
	
    /**
     * 获取评论
     *
     * @access public
     * @return void
     */
    public function getComment()
    {
		$comment = '';
        $coid = $this->request->filter('int')->coid;
        $coid && $comment = $this->select()
            ->where('coid = ?', $coid)
			->limit(1)
			->fetchRow(array($this, 'push'));

		return $comment ? $comment : '';
    }
	
	/** 评论operate */
    public function operate()
    {
		$buttons = array();

		$buttons[] = Helper::element('a', _t('全选'), array(
		    'data-selectall'=>NULL,
			'href'=>'javascript:void(0);'
			)
		);

		$buttons[] = Helper::element('a', _t('删除'), array(
		    'href'=>'javascript:void(0);',
		    'data-action'=>$this->security->actionUrl('comments-edit?do=delete', false),
			'data-confirm'=>_t('删除不可恢复，确定删除？'),
            'data-item'=>'coid',		
			'style'=>'color:red'
			)
		);

		/** 插件接口 */
		//HOOK $buttons[] | PLUG_Comment_Operate($this);
		
		return $this->widget('Widget_Operate')->render($buttons);
    }

    /**
     * 初始化函数
     *
     * @access public
     * @return void
     */
    public function action()
    {
		$this->user->pass('contributor');
		
        $this->security->protect();
		
		$do = $this->request->do;
		
        switch ($this->request->do) {
            case 'delete':
			
                $coids = $this->request->filter('int')->getArray('coid');
                $deleteRows = array();
                foreach ($coids as $coid) 
				{
		            if($this->deleteComment($coid)){ 
					    $deleteRows[] = $coid;
					}
		        }

                /** 设置提示信息 */
                $this->result->set(
				    $deleteRows ? 0 : 1, 
				    $deleteRows ? _t('成功删除 %s 篇评论', count($deleteRows)) : _t('没有评论被删除'),
				    $deleteRows
				);
				
            break;
            case 'get':
			
		        $comment = $this->getComment();
				
				// 获取可编辑的评论
                if ($this->isWriteable()) {
                    $this->result->set(0, $comment);
                } else {
                    $this->result->set(1,  _t('获取评论失败'));
                }
				
            break;

            case 'update':
			
			    $coid = $this->request->filter('int')->coid;
				$updateRows = false;
                $select = $this->select()
                    ->where('coid = ?', $coid)
			        ->limit(1)
			        ->fetchRow(array($this, 'push'));
			
			    if ($this->isWriteable()) {
                    $comments = array(
                        'text' => $this->request->text
                    );

		            $updateRows = $this->updateComment($comments);
				}

                if ($updateRows) {
                    $this->result->set(0, $this->row);

                } else {
                    $this->result->set(1, _t('修评论失败'));
                }

            break;
			
            case 'reply':
			
                $coid = $this->request->filter('int')->coid;
                $parent = $this->select()
                    ->where('coid = ?', $coid)
			        ->limit(1)
			        ->fetchRow();

                /** 评论关闭 */
                if ($parent && 
				    !$this->widget('Widget_Contents_List@'.$parent['cid'], array('cid'=>$parent['cid']))->allow('comment')) {
                    $this->result->set(1,  _t('对不起,此内容的反馈被禁止.'));
                }
				
                $comments = array(
                    'cid'       =>  $parent['cid'],
                    'created'   =>  $this->options->time,
                    'ip'        =>  $this->request->longIp(),
                    'ownerId'   =>  $parent['ownerId'],
                    'uid'       =>  $this->user->uid,
                    'parent'    =>  $coid,
                    'text'      =>  $this->request->text
                );

		        $commentId = $this->insertComment($comments);
			
                if ($commentId) {
                    $this->result->set(0, $this->row);
                } else {
                    $this->result->set(1, _t('回复评论失败'));
                }
			
            break;
			
        }
		$this->result->set(404, _t('请求的资源不存在'));
    }
}
